package com.firstgroup.dms.service.impl;

import com.firstgroup.dms.common.Result;
import com.firstgroup.dms.mapper.StatisticsMapper;
import com.firstgroup.dms.service.StatisticsService;
import com.firstgroup.dms.vo.StockStatVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StatisticsServiceImpl implements StatisticsService {

    @Resource
    private StatisticsMapper statisticsMapper;

    @Override
    public Result getInStockStats(String goodsName, Date startDate, Date endDate, Integer start, Integer limit) {
        try {
            List<StockStatVO> list = statisticsMapper.selectInStockStats(goodsName, startDate, endDate, start, limit);
            Integer total = statisticsMapper.selectInStockStatsCount(goodsName, startDate, endDate);
            Map<String, Object> data = new HashMap<>();
            data.put("list", list);
            data.put("total", total);
            return Result.success(data);
        } catch (Exception e) {
            return Result.error("查询入库统计失败：" + e.getMessage());
        }
    }

    @Override
    public Result getSalesStats(String goodsName, Date startDate, Date endDate, Integer start, Integer limit) {
        try {
            List<StockStatVO> list = statisticsMapper.selectSalesStats(goodsName, startDate, endDate, start, limit);
            Integer total = statisticsMapper.selectSalesStatsCount(goodsName, startDate, endDate);
            Map<String, Object> data = new HashMap<>();
            data.put("list", list);
            data.put("total", total);
            return Result.success(data);
        } catch (Exception e) {
            return Result.error("查询销售统计失败：" + e.getMessage());
        }
    }

    @Override
    public Result getStockQuantity(String goodsName, Integer start, Integer limit) {
        try {
            List<StockStatVO> list = statisticsMapper.selectStockQuantity(goodsName, start, limit);
            Integer total = statisticsMapper.selectStockQuantityCount(goodsName);
            Map<String, Object> data = new HashMap<>();
            data.put("list", list);
            data.put("total", total);
            return Result.success(data);
        } catch (Exception e) {
            return Result.error("查询库存失败：" + e.getMessage());
        }
    }
}